package com.example.xiaosen.controller;

import com.example.xiaosen.repository.ChatHistoryRepository;
import com.example.xiaosen.tools.CourseTools;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @Author: zhouxj@ferrotec.com.cn
 * @version: v1.0
 * @Date: 2025/5/13 15:29
 * @Description:
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/ai")
public class CourseAgentController {

    private final ChatClient agentClient;
    private final ChatHistoryRepository chatHistoryRepository;
    private final CourseTools courseTools;

    /**
     * 基于Function Call实现大模型与数据库交互，一个推荐预约课程的智能助理实现
     * @param prompt
     * @param chatId
     * @return
     */
    @RequestMapping(value = "/service", produces = "text/html;charset=utf-8")
    public Flux<String> service(String prompt, String chatId) {
        // 1.保存会话id
        chatHistoryRepository.save("service", chatId);
        // 2.请求模型
        return agentClient.prompt()
                .user(prompt)
                .tools(courseTools)
                .advisors(a -> a.param(ChatMemory.CONVERSATION_ID,
                        chatId))
                .stream()
                .content();
    }

}
